import request from '@/utils/request'
import { AxiosPromise } from 'axios'
import { PmProjectVO, PmProjectQuery, PmProjectForm } from './types'

/**
 * 查询项目管理列表
 * @param query
 * @returns {*}
 */

export const listProject = (query?: PmProjectQuery): AxiosPromise<PmProjectVO[]> => {
  return request({
    url: '/pm/project/list',
    method: 'get',
    params: query
  })
}

/**
 * 查询项目管理详细
 * @param projectId
 */
export const getProject = (projectId: string | number): AxiosPromise<PmProjectVO> => {
  return request({
    url: '/pm/project/' + projectId,
    method: 'get'
  })
}

/**
 * 新增项目管理
 * @param data
 */
export const addProject = (data: PmProjectForm) => {
  return request({
    url: '/pm/project',
    method: 'post',
    data: data
  })
}

/**
 * 修改项目管理
 * @param data
 */
export const updateProject = (data: PmProjectForm) => {
  return request({
    url: '/pm/project',
    method: 'put',
    data: data
  })
}

/**
 * 删除项目管理
 * @param projectId
 */
export const delProject = (projectId: string | number | Array<string | number>) => {
  return request({
    url: '/pm/project/' + projectId,
    method: 'delete'
  })
}

/**
 * 更新项目进度
 * @param projectId
 * @param progress
 */
export const updateProjectProgress = (projectId: string | number, progress: number) => {
  return request({
    url: '/pm/project/' + projectId + '/progress',
    method: 'put',
    params: { progress }
  })
}

/**
 * 获取项目统计
 * @param projectId
 */
export const getProjectStats = (projectId: string | number): AxiosPromise<PmProjectVO> => {
  return request({
    url: '/pm/project/' + projectId + '/stats',
    method: 'get'
  })
}

/**
 * 获取用户项目
 */
export const getMyProjects = (): AxiosPromise<PmProjectVO[]> => {
  return request({
    url: '/pm/project/my-projects',
    method: 'get'
  })
}

/**
 * 复制项目
 * @param projectId
 * @param newProjectName
 */
export const copyProject = (projectId: string | number, newProjectName: string) => {
  return request({
    url: '/pm/project/' + projectId + '/copy',
    method: 'post',
    params: { newProjectName }
  })
}

/**
 * 归档项目
 * @param projectId
 */
export const archiveProject = (projectId: string | number) => {
  return request({
    url: '/pm/project/' + projectId + '/archive',
    method: 'put'
  })
}

// 团队成员相关API
export const listProjectMember = (query?: any) => {
  return request({
    url: '/pm/team/member/list',
    method: 'get',
    params: query
  })
}

export const getProjectMember = (memberId: number) => {
  return request({
    url: `/pm/team/member/${memberId}`,
    method: 'get'
  })
}

export const addProjectMember = (data: any) => {
  return request({
    url: '/pm/team/member',
    method: 'post',
    data: data
  })
}

export const updateProjectMember = (data: any) => {
  return request({
    url: '/pm/team/member',
    method: 'put',
    data: data
  })
}

export const delProjectMember = (memberIds: number[]) => {
  return request({
    url: `/pm/team/member/${memberIds.join(',')}`,
    method: 'delete'
  })
}

/**
 * 获取项目成员列表
 * @param projectId
 */
export const getProjectMembers = (projectId: string | number) => {
  return request({
    url: `/pm/project/${projectId}/members`,
    method: 'get'
  })
}
